<?php

namespace Util;

class Message {

    /**
     * 发送验证码
     *
     * @param integer $phone 手机号码.
     * @param string  $code  验证码.
     *
     * @return bool
     */
    public static function sendVerify($phone, $code) {
        $content = '您的验证码为：' . $code . '，10分钟内有效，如非本人操作请忽略！';
        $result = self::request($phone, $content);
        return $result;
    }

    private static function request($phone, $content) {
        $post = array();
        $post['userid'] = 11566;
        $post['account'] = 'tianfubiaoju';
        $post['password'] = 'deng0521';
        $post['content'] = $content;
        $post['mobile'] = $phone;
        $url = 'http://www.duanxin10086.com/sms.aspx?action=send';
        $o = '';
        foreach ($post as $k => $v) {
            $o .= $k . '=' . urlencode($v) . '&';
        }
        $post = substr($o, 0, -1);
        $result = \Lib\Util\Comm::request($url, $post, 10);
        if (!$result) {
            // 接口响应超时

            return false;
        }
        $xml = (array) simplexml_load_string($result);
        if (!$xml) {
            // XML解析失败

            return false;
        }
        $status = !empty($xml['returnstatus']) && strtolower($xml['returnstatus']) == 'success' ? true : false;
        $message = !empty($xml['message']) ? $xml['message'] : '';
        $remainPoint = !empty($xml['remainpoint']) ? $xml['remainpoint'] : 0;
        $taskID = !empty($xml['taskID']) ? $xml['taskID'] : 0;
        $successCounts = !empty($xml['successCounts']) ? $xml['successCounts'] : 0;
        if (!$status) {

            return false;
        }
        return true;
    }

}